﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using DBSourceToolsLib;
using DBSourceToolsLib.Interfaces;
using DBSourceToolsLib.SysObjects;
using DBConnectionLib;


namespace DBSourceTools
{
    public partial class SrvExplorer : MainPanelForm, DBSourceToolsLib.Interfaces.IDBExplorerSubject
    {
        List<DBSourceToolsLib.Interfaces.IDBExplorerObserver> m_ListObservers
            = new List<DBSourceToolsLib.Interfaces.IDBExplorerObserver>();

        public SrvExplorer()
        {
            InitializeComponent();
        }

        private void SrvExplorer_Load(object sender, EventArgs e)
        {
            // TODO : COMPLETE 1.2.0.8 server explorer crashes. (kamal101)
            // TODO : list tables, views, etc. (rozentalsn)
            ReloadDatabaseList();
        }

        public void ReloadDatabaseList()
        {
            m_treeView.Nodes.Clear();
            if (UserSettings.Instance().DatabaseProject != null)
            {
                UserSettings.Instance().DatabaseProject.LoadServerList();
                Hashtable oServerList = new Hashtable();

                foreach (DBConnectionLib.Database oDb in UserSettings.Instance().DatabaseProject.ServerList)
                {
                    if (!oServerList.ContainsKey(oDb.DataSourceName))
                        oServerList.Add(oDb.DataSourceName, oDb);
                    //m_listView.Items.Add(oDb.DataSourceName);
                }
                foreach (string strKey in oServerList.Keys)
                {
                    DBConnectionLib.Database oDb = (DBConnectionLib.Database)oServerList[strKey];
                    TreeNode dbNode = m_treeView.Nodes.Add(oDb.DataSourceName, oDb.DataSourceName, 0, 0);
                    dbNode.Tag = oDb;
                }
            }
            else
            {
                MessageBox.Show("Please load or create a Database Project before using Server Explorer");
            }

        }

        #region IDBExplorerSubject Members

        public void RegisterInterestInDbExplorer(DBSourceToolsLib.Interfaces.IDBExplorerObserver observer)
        {
            this.m_ListObservers.Add(observer);
        }

        #endregion

        private void m_treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                if (e.Node.Tag != null)
                {
                    DBConnectionLib.Database oDb = (DBConnectionLib.Database)e.Node.Tag;
                    e.Node.Nodes.Clear();
                    List<string> oDbList = UserSettings.Instance().DatabaseProject.ListDatabasesOnServer(oDb);
                    foreach (string oDatabaseName in oDbList)
                    {
                        TreeNode oNewNode = e.Node.Nodes.Add(oDatabaseName, oDatabaseName, 1, 1);
                    }
                    e.Node.ExpandAll();
                }
            }
            catch (Exception ex)
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oErr = new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(ex.ToString());
                oErr.ShowDialog(this);
            }
            this.Cursor = Cursors.Default;

        }
    }
}
